package com.delivery.controller.admin;

import com.delivery.dto.DishDTO;
import com.delivery.dto.DishPageQueryDTO;
import com.delivery.entity.Dish;
import com.delivery.result.PageResult;
import com.delivery.result.Result;
import com.delivery.service.DishService;
import com.delivery.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @CacheEvict(cacheNames = "dish", key = "#dishDTO.categoryId")
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.addDish(dishDTO);

        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dto
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> paginateDishes(DishPageQueryDTO dto){
        PageResult pageResult = dishService.paginateDishes(dto);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result deleteBatchDishes(@RequestParam List<Long> ids){
        log.info("删除菜品：{}", ids);
        dishService.deleteBatchDishes(ids);
        return Result.success();
    }

    /**
     * 根据ID查询菜品
     * 修改菜品的表单 数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        DishVO dishVO = dishService.getDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO 里面的id 就是要修改的菜品的id
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "dish", allEntries = true)
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}", dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<Dish>> getDishesByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品：{}", categoryId);
        List<Dish> dishes = dishService.getDishesByCategoryId(categoryId);
        return Result.success(dishes);
    }

}
